<?php
require_once 'MDB/QueryTool.php';

class generic_data extends MDB_QueryTool {

    protected $dsn          =   false;
    protected $dbh_options  =   false;
    protected $dbh          =   false;
    protected $database     =   false;
    
    // query tool specific attributes
    var $table          =   false;
    var $sequenceName   =   'generic';

    // this is default, but to demonstrate it here ...
    var $primaryCol     =   false;

    /**
     * This table spec assigns a short name to a table name
     * this short name is needed in case the table name changes
     * i.e. when you put the application on a provider's db, where you have to
     * prefix each table, and you dont need to change the entire application to
     * where you refer to joined table columns, for that joined results the
     * short name is used instead of the table name (ie: alias)
     */
    var $tableSpec = array(
        array('name'  => 'table_name', 'shortName' =>  'bananna'),
        //array('name'  => TABLE_TIME, 'shortName' =>  'time'),
    );

    
    function __construct($options = array()) {
        foreach($options as $attribute => $value) {
            if(isset($this->$attribute)) {
                $this->$attribute    =   $value;
            }
        }
        
        try {
            $this->set_querytool_attributes();
            //$this->set_database_handle();
        } catch(Exception $e) {
            echo 'Caught exception: ',  $e->getMessage(), "\n";
            die();
        }
        
        
    }
    
    /** set_querytool_attributes
    *
    *   In order for the query tool to automagically create SQL, these attributes
    *   need to be set.  This is designed to quickly override in the event a user
    *   may want to change the DSN within the scope of a single instance.
    *
    *   @access public
    *   @param str
    *   @return none
    */
    function set_querytool_attributes($table = false, $primary_col = false) {
        
        $this->table        =   ($table) ? $table : $this->table;
        $this->primaryCol   =   ($primary_col) ? $primary_col : $this->primaryCol;
        
        $this->sequenceName =   $this->table.'_seq';

        $this->tableSpec    =   array(  'name'      =>  $this->table, 
                                        'shortName' =>  $this->table
                                        );
         
        // in case database isn't set in DSN, set it explicitly from property
        if(empty($this->dsn['database']) && $this->database) {
            $this->dsn['database']  =   $this->database;
        }
                                       
        // call parent constructor, PHP 4 styles!  Oh nostalgia 
        parent::MDB_QueryTool($this->dsn, array(), 2); // dsn, options, MDB version
    }
    
    function set_database_handle() {
        require_once 'MDB2.php';
        
        if(!$this->dsn) {
            throw new Exception('Connot connect to database.  No DSN set.');
        }
       
        $mdb2_options   =   ($this->dbh_options) ? $this->dbh_options : null;
        $this->dbh      =&  MDB2::factory($this->dsn, $mdb2_options);
        $this->dbh->loadModule('Extended', null, false);
        $this->dbh->loadModule('Manager');
        $this->dbh->loadModule('Reverse');
        
        if (PEAR::isError($this->dbh)) {
            throw new Exception('Connot connect to database. MDB2 Message: '.$this->dbh->getMessage());
        }
    }
    
    function __destruct() {
        if($this->dbh) {
            $this->dbh->disconnect();
        } 
    }
    
    function get_table_data($table) {
        
        
    }

}

?>

